package kr.ac.ssu.yoobh17.mPlayer.list_album;

import java.io.File;

import kr.ac.ssu.yoobh17.mPlayer.GlobalClass;
import kr.ac.ssu.yoobh17.mPlayer.R;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class AlbumListAdapter extends BaseAdapter {

	private LayoutInflater	layoutInflater;

	private Context			context;
	private Cursor			cursor;

	private ViewHolder		viewHolder;
	
	
	public AlbumListAdapter(Context context,Cursor cursor) {
		
		this.context 	= context;
		this.cursor 	= cursor;

	 	layoutInflater 	= LayoutInflater.from(context);
	 	
	}
	
	@Override
	public int getCount() {

		// TODO Auto-generated method stub
		return cursor.getCount();
	}

	@Override
	public Object getItem( int position ) {

		// TODO Auto-generated method stub
		
		return 0;
	}

	@Override
	public long getItemId( int position ) {
		// TODO Auto-generated method stub

		cursor.moveToPosition( position );
		
		return cursor.getLong( 0 );
	}

	@Override
	public View getView( int position, View convertView, ViewGroup parent ) {

		// TODO Auto-generated method stub
		
		if( null == convertView ){
			
			viewHolder = new ViewHolder();
					
			convertView = layoutInflater.inflate( R.layout.listview_item_album, null );
			
			viewHolder.imageView_albumArt 	= (ImageView) 	convertView.findViewById( R.id.imageView_album_art_in_listview );
			viewHolder.textView_albumTitle 	= (TextView)	convertView.findViewById( R.id.textView_album_item );
			viewHolder.textView_albumArtist = (TextView) 	convertView.findViewById( R.id.textView_album_artist_item );
			
			convertView.setTag( viewHolder );
			
		}
		else{
			
			viewHolder = (ViewHolder) convertView.getTag();
			
		}
		
		cursor.moveToPosition( position );

		final long 	albumId 	= cursor.getInt		( 0 );
		String 		albumTitle 	= cursor.getString	( 1 );
		String 		albumArtist	= cursor.getString	( 2 );

		viewHolder.textView_albumTitle	.setText(albumTitle);
		viewHolder.textView_albumArtist	.setText(albumArtist);
		
		if (viewHolder.task != null) {
			
			viewHolder.task.cancel( true );
			viewHolder.imageView_albumArt.setImageURI( GlobalClass.DEFAUL_ALBUM_ART_URI );
			viewHolder.task = null;
			
		}
		
		viewHolder.task = new AsyncTask<ViewHolder, Void, Uri>() {
			
			private ViewHolder viewHolder;

			@Override
			protected Uri doInBackground( ViewHolder... params ) {

				// TODO Auto-generated method stub
				
				viewHolder = params[0];
				
				return GlobalClass.getAlbumArtUri( context, albumId );
			}

			@Override
			protected void onPostExecute( Uri result ) {

				// TODO Auto-generated method stub
				if (viewHolder.task == this) {
				
					viewHolder.imageView_albumArt.setImageURI( result );
					viewHolder.imageView_albumArt.startAnimation( AnimationUtils.loadAnimation( context, R.anim.anim_fade_in_album_list ) );
					
					viewHolder.task = null;
					
				}
				
				super.onPostExecute( result );
			}

		}.execute( viewHolder );
		
		
		return convertView;
	}

	class ViewHolder {

		TextView	textView_albumTitle;
		TextView	textView_albumArtist;
		ImageView	imageView_albumArt;
		
		AsyncTask	task;

	}

}
